package com.fivium.bpd;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.BevelBorder;

import com.fivium.bpd.component.BusinessProcessDefinition;
import com.fivium.bpd.editor.BPDPanel;
import com.fivium.bpd.editor.MenuBar;
import com.fivium.bpd.io.BPDLoader;

public class BPDEdit
{

  private JFrame frame;
  
  private static JTabbedPane bpdContainer;
  private static JLabel lblStatusLabel;

  /**
   * Launch the application.
   */
  public static void main(final String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        try {
          BPDEdit window = new BPDEdit(args);
          window.frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the application.
   */
  public BPDEdit(String[] args)
  {
    initialize(args);
  }

  /**
   * Initialize the contents of the frame.
   */
  private void initialize(String[] args)
  {
    try {
      // Set System L&F
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } 
    catch (UnsupportedLookAndFeelException e) {
     // handle exception
    }
    catch (ClassNotFoundException e) {
     // handle exception
    }
    catch (InstantiationException e) {
     // handle exception
    }
    catch (IllegalAccessException e) {
     // handle exception
    }
    
    frame = new JFrame();
    frame.setBounds(100, 100, 1000, 800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JMenuBar menuBar = new MenuBar();
    frame.setJMenuBar(menuBar);
    frame.getContentPane().setLayout(new BorderLayout(0, 0));
    
    bpdContainer = new JTabbedPane(JTabbedPane.TOP);
    frame.getContentPane().add(bpdContainer, BorderLayout.CENTER);
    
    lblStatusLabel = new JLabel("Ready");
    lblStatusLabel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    frame.getContentPane().add(lblStatusLabel, BorderLayout.SOUTH);
    
    for (String s : args)
    {
      BPDLoader.loadBusinessProcessDefinition(s);
    }
    
    System.gc();
  }
  
  public void getCurrentBusinessProcessDefinition()
  {
    bpdContainer.getSelectedComponent();
  }
  
  public static void openBusinessProcessDefinition(BusinessProcessDefinition bpd)
  {
    JPanel bpdPanel = new BPDPanel(bpd);
    bpdContainer.addTab(bpd.getFullName(), bpdPanel);
  }
  
  public static void setStatus(String message)
  {
    lblStatusLabel.setText(message);
  }
  
  public static void clearStatus()
  {
    lblStatusLabel.setText("Ready");
  }
}
